package com.itruth.data.dao;

import java.util.LinkedList;
import java.util.List;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.itruth.data.entity.ShareEntity;

public class ShareDao {
	
	public static void insert(ShareEntity entity) {
		
		SQLiteDatabase db = DbHelper.getIns().getWritableDatabase();
		
		db.execSQL(
				"insert into share(content, author, server_id, share_time) values (?, ?, ?, ?)",
				new String[] { entity.getContent(), entity.getAuthor(), String.valueOf(entity.getServerId()), entity.getShareTime()});
		
		db.close();
		
	}
	
	
	public static List<ShareEntity> queryCacheData() {
		
		List<ShareEntity> shares = new LinkedList<ShareEntity>();
		
		SQLiteDatabase db = DbHelper.getIns().getReadableDatabase();
		
		Cursor cursor = db.rawQuery("select * from share", null);
		
		ShareEntity entity = null;
		while (cursor.moveToNext()) {
			entity = new ShareEntity();
			entity.setContent(cursor.getString(cursor.getColumnIndex("content")));
			entity.setShareTime(cursor.getString(cursor.getColumnIndex("share_time")));
			entity.setServerId(cursor.getInt(cursor.getColumnIndex("server_id")));
			entity.setAuthor(cursor.getString(cursor.getColumnIndex("author")));
			shares.add(entity);
		}
		
		db.close();
		
		return shares;
		
		
	}
	
	public static void delete() {
		
		SQLiteDatabase db = DbHelper.getIns().getWritableDatabase();
		
		db.execSQL("delete from share");
		
		db.close();
	}
	
	
}
